#!/usr/bin/perl
#
# this-month - write how many have registered in the Linux Counter
# so far this month, and in 2 previous month.
# Pretty silly early in the month.
#
use Reportsub;
use POSIX;

$nowdate = strftime("%Y-%m-%d", gmtime(time()));
warn "Nowdate is $nowdate\n";

while (<>) {
($date, $country, $sum) = split(":");
# save the last data for each country under "nowdate"
$lastdate = $date;
$sums{$nowdate}{$country} = $sum;
# keep only the first-of-month data
$date = substr($date, 0, 7);
next if $sums{$date}{$country};
$is{$country} = 1;
$sums{$date}{$country} = $sum;
}

# Find the last 3 dates
@dates = sort {$b cmp $a} keys(%sums);
for $datix (0..2) {
for $country (keys(%is)) {
$change = $sums{$dates[$datix]}{$country}
-  $sums{$dates[$datix+1]}{$country};
$change{$country}[$datix] = $change if $change != 0;
}
$showdate[$datix] = $dates[$datix+1];
}
$showdate[0] = substr($dates[0], 0, 7);

getcountryinfo();
print <<EoF;
<?php
include "counter2.php";

pagestart();
?>
<html>
<?php pagehead(_("Recent months' registration rates, per country")) ?>
<BODY bgcolor="white">
<?php pagetop(_("Recent registration rates, per country")) ?>
<?php print _("This list shows how many people registered with the Linux Counter so far this month, and in the two previous months.") ?>
<p>
<?php print _("This list is sorted by registrations so far this month, and unlike the") ?> <a href="short.php"><?php print _("standard report") ?></a>, <?php print _("it is not weighted by country size.") ?>
<p>
<?php print _("This may be a quite weird ranking near the beginning of the month, but is likely to end up with the US on top as the month goes by.") ?>
<p>
<?php print _("This table includes registrations up to") ?> $dates[0].
<p>
<table border>
<tr><th><?php print _("Rank") ?></th><th></th><th><?php print _("Country") ?></th>
EoF
for $datix (0..2) {
    print "<th>", $showdate[$datix], "</th>";
    print "<th><?php print _(\"Rank\") ?></th>" if $datix > 0;
}
print "</tr>\n";

for $n (1..2) {
    $ix = new RankInc;
    for $country (sort({$change{$b}[$n] <=> $change{$a}[$n]} keys(%change))) {
       $ix->incr($change{$country}[$n]) if $country ne "All";
       $rank{$country}[$n] = $ix->show;
    }
}
$rank = new RankInc;
for $country (sort {$change{$b}[0] <=> $change{$a}[0]} keys(%change)) {
    $rank->incr($change{$country}[0]) if $country ne "All";
    print "<tr><td>", $rank->show();
    print "</td><td>", $country,"</td><td>",
    "<a href=\"/reports/place.php?place=$country\">$longname{$country}</a>";
    for $datix (0..2) {
	printf "</td><td>%d", $change{$country}[$datix];
        if ($datix > 0) {
            printf "</td><td>(%d)", $rank{$country}[$datix];
        }
    }
    print "</tr>\n";
}

print <<EoF;
</table>

<?php pagebottom() ?>
</body>
</html>
EoF

#------- Fun stuff: An incrementor for ranking lists ---------
package RankInc;

sub new {
    my $class = shift;
    my $first = shift;
    my $self = {};
    if (defined($first)) {
	$self->{value} = $first;
	$self->{count} = 1;
	$self->{show} = 1;
    } else {
	$self->{count} = 0;
	$self->{show} = 0;
    }
    bless $self;
    $self;
}

sub incr {
    my $self = shift;
    my $val = shift;
    ++ $self->{count};
    if ($val ne $self->{value}) {
	$self->{show} = $self->{count};
	$self->{value} = $val;
    }
}

sub show {
    my $self = shift;
    $self->{show};
}
